package com.imhouxy.springTemplateQuery.query;

import lombok.Getter;

import java.util.Map;
import java.util.UUID;

public class Condition implements Criteria {
    private String sqlFragment;
    @Getter
    private final Map<String, Object> param;

    private Condition(String columnName, String relation) {
        this.sqlFragment = columnName + relation;
        this.param = Map.of();
    }

    private Condition(String where){
        this.sqlFragment = where;
        this.param = Map.of();
    }

    private Condition(String columnName, String relation, Object value) {
        String paramKey = "Param_" + UUID.randomUUID().toString().replace("-", "");
        this.sqlFragment = columnName + relation + ":" + paramKey;
        this.param = Map.of(paramKey, value);
    }

    @Override
    public String sqlFragment() {
        return sqlFragment;
    }

    @Override
    public Map<String, Object> param() {
        return param;
    }

    public static Condition eq(String column, Object value) {
        return new Condition(column, " = ", value);
    }

    public static Condition ne(String column, Object value) {
        return new Condition(column, " != ", value);
    }

    public static Condition like(String column, Object value) {
        return new Condition(column, " like ", "%" + value + "%");
    }

    public static Condition start(String column, Object value) {
        return new Condition(column, " like ", value + "%");
    }

    public static Condition end(String column, Object value) {
        return new Condition(column, " like ", "%" + value);
    }

    public static Condition isNull(String column) {
        return new Condition(column, " is null");
    }

    public static Condition notNull(String column) {
        return new Condition(column, " is not null");
    }

    public static Condition ge(String column, Object value) {
        return new Condition(column, " >= ", value);
    }

    public static Condition gt(String column, Object value) {
        return new Condition(column, " > ", value);
    }

    public static Condition le(String column, Object value) {
        return new Condition(column, " <= ", value);
    }

    public static Condition lt(String column, Object value) {
        return new Condition(column, " < ", value);
    }

    public static Condition sql(String sql){
        return new Condition(sql);
    }

}
